package utils

import (
	"context"
	"fmt"
	"github.com/redis/go-redis/v9"
	"user/models"
)

var redisClient *redis.Client

func init() {
	redisClient = redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "", // 没有密码，默认值
		DB:       0,  // 默认DB 0
	})
}

func Lpush(s []models.Sort) {
	for _, v := range s {
		redisClient.ZAdd(context.Background(), "top", redis.Z{
			Score:  v.Price,
			Member: v.Username,
		})
	}
}

func ZSort() ([]string, error) {
	slice, err := redisClient.ZRevRange(context.Background(), "top", 0, 9).Result()
	fmt.Println(slice)
	if err != nil {
		return []string{}, err
	}
	return slice, nil
}
